/**
 * 
 */

function updateChecked(status, state) {
	$("input[name=checkbox" + state + "]").each(function() {
		this.checked = status;
	});
}
function checkAll(state) {
	updateChecked($("#checkAll" + state).is(':checked'), state);
}

$(document).ready(function() {
	getAccountInfos();
	loadTab(1);
	//Amination for menu logout
	$("#accordian h3").click(function(){
		//slide up all the link lists
		$("#accordian ul ul").slideUp();
		//slide down the link list below the h3 clicked - only if its closed
		if(!$(this).next().is(":visible"))
		{
			$(this).next().slideDown();
		}
	})

});
function updateAccountInfo() {
	$.ajax({
		type : "POST",
		url : "json/accountinfo/updateAccountInfo",
		data : {
			"accountIdEdit" : $('#accountIdEdit').val(),
			"accountNumberEdit" : $('#accountNumberEdit').val(),
			"identityCardEdit" : $('#identityCardEdit').val(),
			"firstNameEdit" : $('#firstNameEdit').val(),
			"midNameEdit" : $('#midNameEdit').val(),
			"lastNameEdit" : $('#lastNameEdit').val(),
			"phone1Edit" : $('#phone1Edit').val(),
			"phone2Edit" : $('#phone2Edit').val(),
			"email1Edit" : $('#email1Edit').val(),
			"email2Edit" : $('#email2Edit').val(),
			"stateEdit" : $('#stateEdit').val()
		},
		dataType : 'json',
		success : function(response) {
			if (response == true) {
				getAccountInfos();
				$('#md_transdetail2').modal('toggle');
			}
		},
		error : function(e) {
		}
	});
}
function getAccountDetail(id) {
	$.ajax({
		type : "GET",
		url : "json/accountinfo/getAccountById/" + id,
		data : null,
		dataType : 'json',
		success : function(response) {
			$('#cusId').html(response.accountId);
			$('#amount').html(response.accountNumber);
			$('#identityCard').html(response.identityCard);
			$('#cusName').html(response.firstName);
			$('#midName').html(response.midName);
			$('#lastName').html(response.lastName);
			$('#phone1').html(response.phone1);
			$('#phone2').html(response.phone2);
			$('#email1').html(response.email1);
			$('#email2').html(response.email2);
			$('#state').html(response.stateName);
		},
		error : function(e) {
			 alert('Error: ' + e);
		}
	});
}
function getAccountEdit(idEdit) {
	$.ajax({
		type : "GET",
		url : "json/accountinfo/getAccountById/" + idEdit,
		data : null,
		dataType : 'json',
		success : function(response) {
			$('#accountIdEdit').val(response.accountId);
			$('#accountNumberEdit').val(response.accountNumber);
			$('#identityCardEdit').val(response.identityCard);
			$('#firstNameEdit').val(response.firstName);
			$('#midNameEdit').val(response.midName);
			$('#lastNameEdit').val(response.lastName);
			$('#phone1Edit').val(response.phone1);
			$('#phone2Edit').val(response.phone2);
			$('#email1Edit').val(response.email1);
			$('#email2Edit').val(response.email2);
			$('#stateEdit').val(response.state);
		},
		error : function(e) {
			alert('Error: ' + e);
		} 
	});
}
function getAccountInfos() {
	$.ajax({
		type : "POST",
		url : "json/accountinfo/getAllAccount",
		data : null,
		dataType : 'json',
		contentType : "application/json",

		success : function(response) {

			var content = "";
			$.each(response, function(i, post) {
				content += "<tr role='row' class='odd'>";
				content += "<td class='sorting_1'>";
				content += post.accountNumber;
				content += "</td>";

				content += "<td>";
				content += post.identityCard;
				content += "</td>";

				content += "<td>";
				content += post.firstName;
				content += "</td>";

				content += "<td>";
				content += post.lastName;
				content += "</td>";

				content += "<td>";
				content += post.phone1;
				content += "</td>";

				content += "<td>";
				content += post.email1;
				content += "</td>";

				content += "<td style=\"text-align:center\">";
				content += post.stateName;
				content += "</td>";

				content += "<td style=\"text-align:center\"><button type=\"button\"";
				content += "onclick=\"getAccountDetail(" + post.accountIdEdit
						+ ")\"";
				content += "class=\"btn btn-default\" data-toggle=\"modal\"";
				content += "data-target=\"#md_transdetail\"";
				content += "value=\"\">Detail</button></td>";

				content += "</tr>";

			});

			$('#tbodyAccount').html(content);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('Error:  ');
		}
	});

}

function loadTab(stateId) {
	$.ajax({
		type : "POST",
		url : "json/accountinfo/getAccountsByState",
		data : {
			"stateId" : stateId
		},
		dataType : 'json',
		success : function(response) {
			var content = "";
			$("#checkAllArea" + stateId).html(
					"<input type=\"checkbox\" onchange=\"checkAll(" + stateId
							+ ")\" id='checkAll" + stateId + "'/>");
			$.each(response, function(i, post) {

				content += "<tr role='row' class='odd'>";
				content += "<td><input type='checkbox' value='"
						+ post.accountId + "' name='checkbox" + stateId
						+ "' class='checkbox" + stateId + "'/></td>";
				content += "<td class='sorting_1'>";
				content += post.accountNumber;
				content += "</td>";

				content += "<td>";
				content += post.firstName;
				content += "</td>";

				content += "<td>";
				content += post.lastName;
				content += "</td>";

				content += "<td>";
				content += post.state;
				content += "</td>";

				content += "</tr>";

			});
			switch (stateId) {
			case 1:
				$("#tbodyNewTab").html(content);
				break;

			case 2:
				$("#tbodyActiveTab").html(content);
				break;

			case 4:
				$("#tbodyDisableTab").html(content);
				break;

			case 3:
				$("#tbodyMarkAsDisableTab").html(content);
				break;

			default:
				break;
			}
		},
		error : function(e) {
		}
	});
}
function transferState(stateId, currentTab) {
	var matches = [];
	$(".checkbox" + currentTab + ":checked").each(function() {
		matches.push(this.value);
	});

	jQuery.each(matches, function(index, value) {
		$.ajax({
			type : "POST",
			url : "json/accountinfo/transferState",
			data : {
				"accountId" : value,
				"stateId" : stateId
			},
			dataType : 'json',
			success : function(response) {
				if (response == true) {
					loadTab(currentTab);
				}
			},
			error : function(e) {
				alert('Error: transferState');
			}
		});
	});
}
function removeAccountInfo(stateId, currentTab) {
	var matches = [];
	$(".checkbox" + currentTab + ":checked").each(function() {
		matches.push(this.value);
	});

	jQuery.each(matches, function(index, value) {
		$.ajax({
			type : "POST",
			url : "json/accountinfo/removeAccountInfo",
			data : {
				"accountId" : value,
			},
			dataType : 'json',
			success : function(response) {
				if (response == true) {
					loadTab(currentTab);
				}
			},
			error : function(e) {
				alert('Error: transferState');
			}
		});
	});
}
function customerAdmin() {
	$.ajax({
		type : "GET",
		url : "tableDetailAdmin",
		success : function(response) {
			$('.main-content').html(response);
			getAccountInfos();
		},
		error : function(e) {
		}
	});
}
function manageAdmin() {
	$.ajax({
		type : "GET",
		url : "tableStateAdmin",
		success : function(response) {
			$('.main-content').html(response);
			loadTab(1);
		},
		error : function(e) {
		}
	});
}
